package com.ddwl.goods.service.bargain;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.feign.api.ShopApi;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dao.bargain.BargainActivityDao;
import com.ddwl.goods.dao.bargain.BargainSkuDao;
import com.ddwl.goods.model.bargain.BargainActivity;
import com.ddwl.goods.model.bargain.BargainSku;
import com.ddwl.goods.service.ItemService;
import com.ddwl.goods.service.SkuService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.bargain.BoostActivityItemPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.ItemVo;
import com.ddwl.schema.vo.goods.SkuVo;
import com.ddwl.schema.vo.goods.bargain.BargainActivityDetailVo;
import com.ddwl.schema.vo.goods.bargain.BargainActivityItemVo;
import com.ddwl.schema.vo.goods.bargain.BargainItemDetailVo;
import com.ddwl.schema.vo.goods.bargain.BargainSkuDetailVo;
import com.ddwl.schema.vo.shop.ShopVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author hemiaojun
 * @Date 2020/10/29 13:17
 */
@Service
public class BargainItemService {

    @Resource
    private BargainActivityDao bargainActivityDao;
    @Resource
    private BargainSkuDao bargainSkuDao;
    @Resource
    private ItemService itemService;
    @Resource
    private SkuService skuService;
    @Resource
    private ShopApi shopApi;

    public BargainActivityDetailVo getDetailById(String id) {
        BargainActivity ba = bargainActivityDao.selectByPrimaryKey(id);
        if (null == ba) {
            return null;
        }
        BargainActivityDetailVo baVo = BeanConverUtils.convertBean(ba, BargainActivityDetailVo.class);
        ItemVo itemVo=itemService.getItem(ba.getItemId());
        BargainSku bs = new BargainSku();
        bs.setActivityId(id);
        List<BargainSku> bsList=bargainSkuDao.select(bs);
        if(!CollectionUtils.isEmpty(bsList)){
            baVo.setSkuVoList(getBargainSkuDetailVos(bsList));
        }
        baVo.setItemVo(BeanConverUtils.convertBean(itemVo, BargainItemDetailVo.class));
        if (BaseEnum.StatusEnum.ENABLE.getCode().equals(baVo.getSelfMentionTag())) {
            ShopVo shopVo = getShop(ba.getShopId());
            baVo.setShopAddr(shopVo.getAddress());
            baVo.setLng(shopVo.getLng().toString());
            baVo.setLat(shopVo.getLat().toString());
        }
        updateActivityViews(ba);
        return baVo;
    }

    private void updateActivityViews(BargainActivity ba) {
        BargainActivity update = new BargainActivity();
        update.setId(ba.getId());
        int views = null == ba.getViews() ? 0 : ba.getViews();
        update.setViews(views + 1);
        bargainActivityDao.updateByPrimaryKeySelective(update);
    }

    public PageVo<BargainActivityItemVo> list(PageBo pageBo) {
        String shopId = getDefaultShopId();
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        List<BargainActivityItemVo> baList = bargainActivityDao.getBargainActivityItemList(shopId);
        for (BargainActivityItemVo vo:baList) {
            Integer virtualSale = null == vo.getVirtualSale() ? 0 : vo.getVirtualSale();
            Integer realSale = null == vo.getRealSale() ? 0 : vo.getRealSale();
            vo.setSumSale(virtualSale+realSale);
        }
        PageInfo<BargainActivityItemVo> info = new PageInfo<>(baList);
        return new PageVo<>(info.getTotal(), pageBo.getPage(), pageBo.getPageSize(), baList);
    }

    public List<BargainSkuDetailVo> getBargainSku(String activityId, String itemId) {
        BargainSku bs = new BargainSku();
        bs.setActivityId(activityId);
        bs.setItemId(itemId);
        List<BargainSku> bsList=bargainSkuDao.select(bs);
        if(!CollectionUtils.isEmpty(bsList)){
            return getBargainSkuDetailVos(bsList);
        }
        return null;
    }

    private List<BargainSkuDetailVo> getBargainSkuDetailVos(List<BargainSku> bsList) {
        List<String> codeList = bsList.stream().map(bargainSku -> bargainSku.getBarcode()).collect(Collectors.toList());
        List<SkuVo> skuVos= skuService.getByBarCodeList(codeList);
        skuVos.forEach(skuVo -> {
            BargainSku bs = bsList.stream().filter(bargainSku -> skuVo.getBarcode().equals(bargainSku.getBarcode())).findFirst().orElse(null);
            if (null != bs) {
                skuVo.setRealStock(bs.getChopStock());
            }
        });
        return BeanConverUtils.convertBeanList(skuVos, BargainSkuDetailVo.class);
    }

    private String getDefaultShopId(){
        ShopVo shopVo = shopApi.getDefaultShopForApi().getResult();
        ParamException.isNull(shopVo, "店铺信息不存在");
        return shopVo.getId();
    }

    private ShopVo getShop(String shopId) {
        ShopVo shopVo = shopApi.getShopById(shopId).getResult();
        StringBuilder sb = new StringBuilder();
        if (StrUtil.isNotEmpty(shopVo.getProvinceName())) {
            sb.append(shopVo.getProvinceName());
        }
        if (StrUtil.isNotEmpty(shopVo.getCityName())) {
            sb.append(shopVo.getCityName());
        }
        if (StrUtil.isNotEmpty(shopVo.getDistrictName())) {
            sb.append(shopVo.getDistrictName());
        }
        if (StrUtil.isNotEmpty(shopVo.getAddress())) {
            sb.append(shopVo.getAddress());
        }
        shopVo.setAddress(sb.toString());
        return shopVo;
    }

    public PageVo<BargainActivityItemVo> boostList(PageBo<BoostActivityItemPageBo> pageBo) {
        ParamException.isNull(pageBo.getParam().getShopId());
        List<String> shopIds = new ArrayList<>();
        String defaultShopId = getDefaultShopId();
        shopIds.add(pageBo.getParam().getShopId());
        shopIds.add(defaultShopId);
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize());
        List<BargainActivityItemVo> baList = bargainActivityDao.boostList(shopIds);
        PageInfo<BargainActivityItemVo> info = new PageInfo<>(baList);
        return new PageVo<>(info.getTotal(), pageBo.getPage(), pageBo.getPageSize(), baList);
    }
}
